<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '680px' }"
    >
      <p>
        <a-form layout="inline" :model="nameParam">
          <a-form-item>
            <a-button type="primary" @click="handleQuery()" size="large">查询
            </a-button>
          </a-form-item>
          <a-form-item>
            <a-button type="primary" @click="add" size="large">新增分类</a-button>
          </a-form-item>
        </a-form>
      </p>
      <a-table
          v-if="level1.length"
          :columns="columns"
          :row-key="record => record.id"
          :data-source="level1"
          :loading="loading"
          :pagination="false"
          :defaultExpandAllRows="true"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'cover'">
            <a>
              <a-avatar :src="record.cover"/>
            </a>
          </template>
          <template v-else-if="column.key === 'action'">
            <span>
              <a-space size="small">
                <a-button type="primary" @click="edit(record)">编辑</a-button>
                <a-popconfirm
                    title="删除后不可恢复，确认删除?"
                    ok-text="是"
                    cancel-text="否"
                    @confirm="handleDelete(record.id)"
                >
                  <a-button type="primary" danger>删除</a-button>
                </a-popconfirm>
              </a-space>
            </span>
          </template>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>
  <a-modal
      v-model:visible="visible"
      title="分类表单"
      :confirm-loading="confirmLoading"
      @ok="handleOk"
      :ok-text=okText
      cancel-text="取消"
  >
    <a-form :model="category" :label-col="{ span: 6 }">
      <a-form-item label="名称">
        <a-input v-model:value="category.name"/>
      </a-form-item>
      <a-form-item label="父分类">
        <a-select
            ref="select"
            v-model:value="category.parent"
        >
          <a-select-option value="0">无</a-select-option>
          <a-select-option v-for="c in level1" :key="c.id" :value="c.id" :disabled="category.id === c.id">
            {{ c.name }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="顺序">
        <a-input-number style="width: 100%;" :min="1" :max="99999" v-model:value="category.sort"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from "axios";
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool";

export default defineComponent({
  name: 'AdminCategory',
  setup() {
    const categorys = ref();
    const nameParam = ref({name: ''});

    const loading = ref(false);

    const columns = [
      {
        title: '名称',
        dataIndex: 'name',
        key: 'name',
      },
      {
        title: '父分类名称',
        dataIndex: 'parentName',
        key: 'parentName',
      },
      {
        title: '父分类id',
        dataIndex: 'parent',
        key: 'parent',
      },
      {
        title: '顺序',
        dataIndex: 'sort',
        key: 'sort'
      },
      {
        title: 'Action',
        dataIndex: 'action',
        key: 'action'
      }
    ];


    const level1 = ref();//树形结构
    level1.value = [];
    /**
     * 数据查询
     */
    const handleQuery = () => {
      loading.value = true;
      axios.get("/category/all").then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          categorys.value = data.content;
          level1.value = [];
          level1.value = Tool.array2Tree2(categorys.value, 0);
        } else {
          message.error(data.message);
        }
      })
    }

    const visible = ref<boolean>(false);
    const confirmLoading = ref<boolean>(false);
    const category = ref({});
    const okText = ref();

    /**
     * 编辑
     * @param record
     */
    const edit = (record: any) => {
      okText.value = "修改";
      visible.value = true;
      //复制一个新对象，这样进行编辑的时候，就不会修改表格数据了
      //category.value = record，这样写就和表格数据绑定了
      category.value = Tool.copy(record);
    }

    /**
     * 新增
     */
    const add = () => {
      okText.value = "新增";
      visible.value = true;
      category.value = {};
    }

    const handleDelete = (id: any) => {
      axios.delete("/category/delete/" + id).then((response) => {
        const data = response.data;
        if (data.success) {
          //重新加载列表
          handleQuery()

          message.success("删除成功");
        }
      })
    }

    const handleOk = () => {
      confirmLoading.value = true;
      axios.post("/category/save", category.value).then((response) => {
        confirmLoading.value = false;
        const data = response.data;
        if (data.success) {
          visible.value = false;

          //重新加载列表
          handleQuery()

          message.success(okText.value + "成功");
        } else {
          message.error(data.message);
        }
      })
    }


    onMounted(() => {
      handleQuery()
    })

    return {
      //categorys,
      columns,
      loading,
      visible,
      confirmLoading,
      edit,
      handleOk,
      category,
      add,
      okText,
      handleDelete,
      nameParam,
      handleQuery,
      level1
    };
  }
});
</script>
<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>
